<?php

namespace Home\Controller;

use Home\Base\Util;
use Think\Controller;

/**
 * @desc 常用功能
 * @author 张云江
 */
class PayController extends InitController
{

    /**
     * 订阅管理页面
     */
    public function dingyue()
    {

        $application_id = $_GET['application_id'];
        $type = $_GET['type'];
        $stxt = $_GET['stxt'];

        if (empty($application_id)) {
            $application_id = 0;
        }

        if ($type < 1 || $type > 2) {
            $type = 1;
        }

        $MemOrder_Application_info = new \Mem\MemOrder_Application_info();
        $applicatonList = $MemOrder_Application_info->getDataWithAll();
        $this->assign("applicationList", $applicatonList);

        $MemOrder_Subscribe_info = new \Mem\MemOrder_Subscribe_info();
        $page_size = 20;

        $total = $MemOrder_Subscribe_info->getCountDataWithAll($application_id, $type, $stxt);
        $pageObj = Util::getPageObject($total, $page_size); //加入分页
        $mempage = $pageObj["mempage"];

        $dataList = $MemOrder_Subscribe_info->getDataWithAll($application_id, $type, $stxt, $mempage);
        $newdataList = array();
        foreach ($dataList as $val) {
            if ($val['state'] == 1) {
                $val['state_show'] = '自动订阅';
            } else {
                $val['state_show'] = '非自动订阅';
            }
            array_push($newdataList, $val);
        }
        $this->assign("dataList", $newdataList);

        $this->assign("application_id", $application_id);
        $this->assign("type", $type); //1.订单号 2.产品渠道
        $this->assign("stxt", $stxt);

        $pageshow = $pageObj["pageshow"];
        $this->assign("page", $pageshow);
        $this->assign("pageindex", $pageObj["mempage"]->getLimitIndex());

        $navPage = array(
            array("name" => "订阅列表", "url" => ""),
        );
        $this->assign("navPage", $navPage);
        $this->assign("menuFlag", "dingyue");
        $this->display();
    }

    /**
     * 订单管理页面
     */
    public function orderlist()
    {

        $application_id = $_GET['application_id'];
        $type = $_GET['type'];
        $stxt = $_GET['stxt'];
        $begin_dt_old = $_GET['begin_dt'];
        $end_dt_old = $_GET['end_dt'];

        $begin_dt = str_replace("'", "", $begin_dt_old);
        $end_dt = str_replace("'", "", $end_dt_old);
        $begin_dt = str_replace("+", " ", $begin_dt);
        $end_dt = str_replace("+", " ", $end_dt);

        if (empty($application_id)) {
            $application_id = 0;
        }

        if ($type < 1 || $type > 2) {
            $type = 1;
        }

        $MemOrder_info = new \Mem\MemOrder_info();
        $MemOrder_info_meta = new \Mem\MemOrder_info_meta();
        $page_size = 20;

        $MemOrder_Application_info = new \Mem\MemOrder_Application_info();
        $applicatonList = $MemOrder_Application_info->getDataWithAll();
        $this->assign("applicationList", $applicatonList);

        $total = $MemOrder_info->getCountDataWithAll($application_id, $type, $stxt, $begin_dt, $end_dt);
        $pageObj = Util::getPageObject($total, $page_size); //加入分页
        $mempage = $pageObj["mempage"];

        $dataList = $MemOrder_info->getDataWithAll($application_id, $type, $stxt, $begin_dt, $end_dt, $mempage);
        $newdataList = array();
        foreach ($dataList as $val) {
            if ($val['source_type'] == 1) {
                $val['show_source'] = '测试';
            } else {
                $val['show_source'] = '生产';
            }
            //加载扩展
            $order_id = $val['order_id'];
            $orderMeta = $MemOrder_info_meta->getDataWithorder_id($order_id);
            $metaConf = array();
            foreach ($orderMeta as $valm) {
                $metaConf[$valm['meta_key']] = $valm['meta_val'];
            }
            $val['show_state'] = '正常';
            if (!empty($metaConf['cancellation_date'])) {
                $val['show_state'] = '失效';
            }
            $val['show_verify_type'] = '订阅';
            if (!empty($metaConf['verify_type'])) {
                $val['show_verify_type'] = '消耗品';
            }
            if ($val['purchase_date'] == $val['expires_date']) {
                $val['show_verify_type'] = '消耗品';
            }
            array_push($newdataList, $val);
        }
        $this->assign("dataList", $newdataList);

        $this->assign("application_id", $application_id);
        $this->assign("type", $type); //1.订单号 2.产品渠道
        $this->assign("stxt", $stxt);
        $this->assign("begin_dt", $begin_dt);
        $this->assign("end_dt", $end_dt);

        $pageshow = $pageObj["pageshow"];
        $this->assign("page", $pageshow);
        $this->assign("pageindex", $pageObj["mempage"]->getLimitIndex());

        $navPage = array(
            array("name" => "订单列表", "url" => ""),
        );
        $this->assign("navPage", $navPage);
        $this->assign("menuFlag", "orderlist");
        $this->display();
    }

    /**
     * 订阅管理页面
     */
    public function noticelist()
    {

        $application_id = $_GET['application_id'];

        if (empty($application_id)) {
            $application_id = 0;
        }

        $MemOrder_Application_info = new \Mem\MemOrder_Application_info();
        $applicatonList = $MemOrder_Application_info->getDataWithAll();
        $this->assign("applicationList", $applicatonList);

        $MemOrder_Notice_info = new \Mem\MemOrder_Notice_info();
        $page_size = 20;

        $total = $MemOrder_Notice_info->getCountDataWithAll($application_id);
        $pageObj = Util::getPageObject($total, $page_size); //加入分页
        $mempage = $pageObj["mempage"];

        $dataList = $MemOrder_Notice_info->getDataWithAll($application_id, $mempage);
        $newdataList = array();
        foreach ($dataList as $val) {
            if ($val['environment'] == "PROD") {
                $val['environment_show'] = '生产';
            } else {
                $val['environment_show'] = '测试';
            }
            if ($val['notification_type'] == "INITIAL_BUY") {
                $val['notification_show'] = '初次购买订阅';
            } else if ($val['notification_type'] == "CANCEL") {
                $val['notification_show'] = '用户已退款';
            } else if ($val['notification_type'] == "RENEWAL") {
                $val['notification_show'] = '更新已过期订阅成功';
            } else if ($val['notification_type'] == "INTERACTIVE_RENEWAL") {
                $val['notification_show'] = 'App Store 交互方式续订订阅';
            } else if ($val['notification_type'] == "DID_CHANGE_RENEWAL_PREF") {
                $val['notification_show'] = '客户更改订购计划下次生效';
            } else if ($val['notification_type'] == "DID_RENEW") {
                $val['notification_show'] = '自动续订成功';
            } else if ($val['notification_type'] == "DID_CHANGE_RENEWAL_STATUS") {
                if ($val['auto_renew_status'] == 1) {
                    $val['notification_show'] = '订阅状态改变-自动续订';
                } else {
                    $val['notification_show'] = '订阅状态改变-非自动续订';
                }
            } else if ($val['notification_type'] == "DID_FAIL_TO_RENEW") {
                $val['notification_show'] = '由于计费问题而无法续订';
            } else if ($val['notification_type'] == "DID_RECOVER") {
                $val['notification_show'] = '更新已过期订阅成功';
            } else if ($val['notification_type'] == "PRICE_INCREASE_CONSENT") {
                $val['notification_show'] = '客户同意您的应用的订阅价格上涨';
            } else if ($val['notification_type'] == "REFUND") {
                $val['notification_show'] = '成功退还可消耗的应用内购买交易';
            } else if ($val['notification_type'] == "REVOKE") {
                $val['notification_show'] = '用户不再通过“家庭共享”获得应用内购买';
            } else if ($val['notification_type'] == "CONSUMPTION_REQUEST") {
                $val['notification_show'] = '用户发起退款申请';
            } else {
                $val['notification_show'] = $val['notification_type'];
            }
            array_push($newdataList, $val);
        }
        $this->assign("dataList", $newdataList);
        $this->assign("application_id", $application_id);

        $pageshow = $pageObj["pageshow"];
        $this->assign("page", $pageshow);
        $this->assign("pageindex", $pageObj["mempage"]->getLimitIndex());

        $navPage = array(
            array("name" => "消息列表", "url" => ""),
        );
        $this->assign("navPage", $navPage);
        $this->assign("menuFlag", "noticelist");
        $this->display();
    }
}
